import { parse } from 'querystring';
export const getPageQuery = () => parse(window.location.href.split('?')[1]);

export function getUrlParam(name: string) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|)'); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search
        .trim()
        .substr(1)
        .match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    else return ''; //返回参数值
}

export async function queryCurrentUser(): Promise<any> {
    return new Promise((resolve, reject) => {
        const parmas = getPageQuery();
        let { token, userid, openid } = parmas;
        userid = parseInt(userid as string);
        if (token && userid && openid) {
            resolve({ token, userid, openid });
        } else {
            reject(false);
        }
    });
}
